package com.base.webapp.respm;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import cn.winwintech.Incense.util.StringUtil;
import cn.winwintech.Incense.util.WeatherHelper;

public class WeatherInfo {

	public static final String DEGREE = "\u2103";
	private static final String IMAGE_URL_BASE = "http://m.weather.com.cn/img/";
	public String currentCity;
	public String dayPictureUrl;
	public String weather;
	// 湿度
	private String humidity;
	// 风向
	private String wind;
	public String nightPictureUrl;
	public String tempMax;
	public String tempMin;
	private boolean isSameTemp = false;
	private boolean success = false;

	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	public WeatherInfo(String result, String result2) {
		parse(result);
		getOtherData(result2);
	}

	public void parse(String json) {
		try {

			JSONObject root = new JSONObject(json);
			JSONObject f = root.getJSONObject("f");
			System.out.println(root);
			JSONArray f1 = f.getJSONArray("f1");
			JSONObject c = root.getJSONObject("c");
			JSONObject info = f1.getJSONObject(0);
			this.currentCity = c.getString("c3");
			String dayCode = info.getString("fa"); // 白天天气
			String nightCode = info.getString("fb");// night??
			String weatherCode = StringUtil.isBlank(dayCode) ? nightCode : dayCode;
			this.weather = WeatherHelper.getWeather(weatherCode);
			String tmpMax = info.getString("fc"); // 白天温度
			String tmpMin = info.getString("fd"); // 晚上气温
			if (StringUtil.isBlank(tmpMax) || StringUtil.isBlank(tmpMin)) {
				isSameTemp = true;
			}
			this.tempMax = (StringUtil.isBlank(tmpMax) ? tmpMin : tmpMax);
			this.tempMin = (StringUtil.isBlank(tmpMin) ? tmpMax : tmpMin);
			success = true;
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

	public void getOtherData(String json) {
		try {
			JSONObject root = new JSONObject(json);
			JSONObject weatherinfo = root.getJSONObject("weatherinfo");
			System.out.println(weatherinfo);
			this.setHumidity(weatherinfo.getString("SD")); // 湿度
			this.setWind(weatherinfo.getString("WD") + " " + weatherinfo.getString("WS"));
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

	public String getTitle() {
		return currentCity + "   " + weather;
	}

	public String getTemperature() {
		if (isSameTemp) {
			return tempMax + DEGREE;
		}
		return tempMin + DEGREE + " ~ " + tempMax + DEGREE;
	}

	@Override
	public String toString() {
		return "WeatherInfo [currentCity=" + currentCity + ", dayPictureUrl=" + dayPictureUrl + ", weather=" + weather
				+ ", nightPictureUrl=" + nightPictureUrl + ", tempMax=" + tempMax + ", tempMin=" + tempMin
				+ ", success=" + success + "]";
	}

	public String getHumidity() {
		return humidity;
	}

	public void setHumidity(String humidity) {
		this.humidity = humidity;
	}

	public String getWind() {
		return wind;
	}

	public void setWind(String wind) {
		this.wind = wind;
	}

}